package net.hn.hnms.biz.mine.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.api.video.RemoteVideoService;
import net.hn.hnms.biz.api.video.model.dto.Camera;
import net.hn.hnms.biz.mine.domain.VideoConfig;
import net.hn.hnms.biz.mine.domain.vo.VideoConfigVo;
import net.hn.hnms.biz.mine.mapper.VideoConfigMapper;
import net.hn.hnms.biz.mine.service.IVideoConfigService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VideoConfigServiceImpl extends ServiceImpl<VideoConfigMapper, VideoConfig> implements IVideoConfigService {

    private final RemoteVideoService remoteVideoService;

    public VideoConfigServiceImpl(RemoteVideoService remoteVideoService) {
        this.remoteVideoService = remoteVideoService;
    }

    @Override
    public void updateByCode(VideoConfig videoConfig) {
        saveOrUpdate(videoConfig, new LambdaUpdateWrapper<VideoConfig>().eq(VideoConfig::getCameraCode, videoConfig.getCameraCode()));
    }

    @Override
    public ResponsePage<VideoConfigVo> selectPageList(VideoConfigVo videoConfigVo, PageQuery pageQuery) {
        R<List<Camera>> list = remoteVideoService.list();
        if (list.getCode() == R.SUCCESS) {
            List<Camera> cameraList = list.getData();
            List<VideoConfig> videoConfigs = baseMapper.selectList(null);
            List<VideoConfigVo> collect = cameraList.stream().map(camera -> {
                VideoConfigVo v = new VideoConfigVo();
                VideoConfig videoConfig1 = videoConfigs.stream().filter(videoConfig -> videoConfig.getCameraCode().equals(camera.getCameraIndexCode())).findFirst().orElse(null);
                v.setCameraCode(camera.getCameraIndexCode());
                v.setCameraRegin(camera.getRegionName());
                v.setCameraRegin(camera.getRegionName());
                v.setCameraName(camera.getCameraName());
                if (videoConfig1 != null) {
                    v.setImportant(videoConfig1.getImportant());
                    v.setVideoRegion(videoConfig1.getVideoRegion());
                }
                v.setOnline(camera.getOnline());
                v.setCollectTime(camera.getCollectTime());
                return v;
            }).collect(Collectors.toList());
            if (StringUtils.isNotBlank(videoConfigVo.getImportant())) {
                collect = collect.stream().filter(videoConfigVo1 -> StringUtils.isNotBlank(videoConfigVo1.getImportant()) && videoConfigVo1.getImportant().equals(videoConfigVo.getImportant())).collect(Collectors.toList());
            }
            if (StringUtils.isNotBlank(videoConfigVo.getCameraName())) {
                collect = collect.stream().filter(videoConfigVo1 -> videoConfigVo1.getCameraName().contains(videoConfigVo.getCameraName())).collect(Collectors.toList());
            }
            if (StringUtils.isNotBlank(videoConfigVo.getVideoRegion())) {
                collect = collect.stream().filter(videoConfigVo1 -> StringUtils.isNotBlank(videoConfigVo1.getImportant()) && videoConfigVo1.getVideoRegion().contains(videoConfigVo.getVideoRegion())).collect(Collectors.toList());
            }
            Integer count = collect.size();
            // 分页
            collect = collect.stream().skip((pageQuery.getPageNum() - 1) * pageQuery.getPageSize()).limit(pageQuery.getPageSize()).collect(Collectors.toList());
            return new ResponsePage<>(collect, count);
        }
        return new ResponsePage<>(Collections.emptyList(), 0);
    }

}
